/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller;

import com.database.dao.BeberageAccessObject;
import com.database.dao.DataAccessObject;
import com.database.tables.DatabaseTables;
import com.domain.Beberage;
import com.domain.BeberagesList;
import com.view.BeberageManagementView;
import com.view.CreateBeberageView;
import com.view.ShowBeberageInformationView;
import java.util.ArrayList;

/**
 *
 * @author Miguel
 */
public class BeberageManager {
    
    public static BeberageManager getInstance(){
        if(INSTANCE == null){
            INSTANCE = new BeberageManager();
        }
        return INSTANCE;
    }
    
    public void registerBeberage(String beberageName,String beberageDescription,CreateBeberageView beberageCreationView){
        if(isBeberageAlreadyStored(beberageName)){
            showMessageToBeberageRegisterForm("La bebida ya existe",beberageCreationView);
        }else{
            Beberage newBeberage = createBeberage(beberageName,beberageDescription);
            boolean isBeberageSuccesfullyStored = storeBeberage(newBeberage);
            if(isBeberageSuccesfullyStored){
                showMessageToBeberageRegisterForm("Bebida almacenada con éxito",beberageCreationView);
                closeBeberageRegisterForm(beberageCreationView);
            }else{
                showMessageToBeberageRegisterForm("Error en almacenamiento de bebida",beberageCreationView);
            }
        }
    }
    
    public void modifyBeberage(Beberage oldBeberage,Beberage newBeberage,ShowBeberageInformationView informationView){
        boolean isBeberageSuccessfullyUpdated = updateBeberageFromInventory(oldBeberage,newBeberage);
        if(isBeberageSuccessfullyUpdated){
            showMessageAndCloseForm("Bebida Actualizada Exitósamente",informationView);
        }else{
            informationView.showMessageToForm("No se pudo Actualizar la bebida");
        }
    }
    
    public BeberagesList getBeberageList(){
        ArrayList<Beberage> beberagesList = beberageDAO.selectDataFromDatabase(null);
        BeberagesList beberageList = new BeberagesList(beberagesList);
        return beberageList;
    }
    
    public boolean removeBeberage(Beberage beberage,BeberageManagementView managementView){
        boolean isBeberageRemovedSuccessfully = removeBeberageFromInventory(beberage);
        if(isBeberageRemovedSuccessfully){
            managementView.showMessageToForm("Bebida Eliminada con éxito");
        }else{
            managementView.showMessageToForm("No se pudo eliminar la bebida");
        }
        return isBeberageRemovedSuccessfully;
    }
    
    public BeberagesList searchForBeberages(String searchCondition){
        ArrayList<Beberage> list = beberageDAO.selectDataFromDatabase(searchCondition);
        BeberagesList beberagesList = new BeberagesList(list);
        return beberagesList;
    }
    
    private void showMessageAndCloseForm(String msg,ShowBeberageInformationView managementView){
        managementView.showMessageToForm(msg);
        managementView.closeInformationForm();
    }
    private boolean updateBeberageFromInventory(Beberage oldBeberage,Beberage newBeberage){
        int updateBeberageResult = beberageDAO.updateObject(oldBeberage, newBeberage);
        return updateBeberageResult != DataAccessObject.ERROR_EXECUTING_OPERATION;
    }
    
    private boolean removeBeberageFromInventory(Beberage deletingBeberage){
        int beberageDeleteResult = beberageDAO.deleteObject(deletingBeberage);
        return beberageDeleteResult != DataAccessObject.ERROR_EXECUTING_OPERATION;
    }
    
    private void closeBeberageRegisterForm(CreateBeberageView creationView){
        creationView.closeBeberageCreationForm();
    }
    
    private void showMessageToBeberageRegisterForm(String msg,CreateBeberageView creationView){
        creationView.showMessage(msg);
    }
    
    private boolean storeBeberage(Beberage storingBeberage){
        int storeBeberageResult = beberageDAO.insertObject(storingBeberage);
        return storeBeberageResult != DataAccessObject.ERROR_EXECUTING_OPERATION;
    }
    
    private boolean isBeberageAlreadyStored(String beberageName){
        boolean beberageExists = false;
        if(searchBeberage(beberageName) != null){
            beberageExists = true;
        }
        return beberageExists;
    }
    
    private Beberage searchBeberage(String beberageName){
        String condition = "BeberageName = '" + beberageName + "'";
        ArrayList<Beberage> beberageSearchResult = beberageDAO.selectDataFromDatabase(condition);
        Beberage searchedBeberage = null;
        if(beberageSearchResult.size() > 0){
            searchedBeberage = beberageSearchResult.get(0);
        }
        return searchedBeberage;
    }
    
    private Beberage createBeberage(String beberageName,String beberageDescription){
        Beberage beberage = new Beberage();
        beberage.setName(beberageName);
        beberage.setDescription(beberageDescription);
        return beberage;
    }
    
   private BeberageManager(){
       beberageDAO = new BeberageAccessObject(DatabaseTables.BEBERAGES_TABLE);
   }
   
    private BeberageAccessObject beberageDAO;
    private static BeberageManager INSTANCE;
}
